<script setup lang="ts">
defineOptions({
  name: "barCharts"
});
import { ref, onMounted, computed, nextTick } from "vue";
import echartsCom from "@/views/common/components/echartsCom/index.vue";
import barTitle from "../barTitle/index.vue";
import type { LabelLayoutFunctionType } from "@/views/common/components/types";

interface Props {
  /**  获取当前echarts实例对象，参数为实例对象和回调函数 */
  // getEchartsInstance?: EchartsInstanceType | null;
  /**  获取 optiins 配置函数 */
  labelLayoutFn?: LabelLayoutFunctionType | null;
  /**  同 echarts option */
  // options?: CommonEcharts.CommonEchartsOption | null;
  optionData?: any;
  /**  有 title 时传入 */
  title?: string;
  /**  有按钮时传入，传入后会有 btn-click 事件 */
  button?: string;
  /**  title 栏右侧文字 */
  rightText?: string | number;
  /**  模块最低宽度，默认 330px */
  width?: number;
  /**  模块最低高度，默认 200px */
  height?: number;
  /**  是否需要背景阴影，默认为true */
  boxShadow?: boolean;
  color?: string;
  /**  是否需要圆角，默认为true */
  borderRidus?: boolean;
  className?: string;
  dateRange?: any;
}

const props = withDefaults(defineProps<Props>(), {
  width: 600,
  height: 468,
  boxShadow: false,
  borderRidus: true
});

interface Emits {
  /**  title 右侧按钮点击后触发 */
  (event: "btn-click"): void;
}

const emits = defineEmits<Emits>();

const echartsKey = ref<string>(Math.random().toString(36).slice(2, 10));
const echartsComRef = ref<InstanceType<typeof echartsCom> | null>(null);

const title_height = computed(() => (props.title ? "38px" : "0px"));
const min_width = computed(() => `${props.width}px`);
const min_height = computed(() => `${props.height}px`);

const options = computed(() => {
  const yAxis = [
    {
      type: "value"
    }
  ];
  const series = [
    {
      data: props.optionData.configList.map(item => item.value || 0) || [],
      // data: [],
      type: "bar",
      label: {
        show: true,
        position: "top",
        formatter: "{c}"
      },
      barWidth: 65,
      itemStyle: {
        color: props.color
      }
    }
  ];
  const option = {
    title: {
      text: props.optionData.sysName + "系统",
      textStyle: {
        fontSize: 16
      },
      subtext: "药剂累计用量（L）",
      left: "left",
      top: "top",
      padding: [10, 0, 0, 20]
    },
    color: [props.color],
    grid: {
      left: "20px",
      top: "70px",
      right: "20px",
      bottom: "10px",
      containLabel: true
    },
    legend: {},
    tooltip: {
      trigger: "axis",
      confine: true,
      axisPointer: {
        type: "shadow"
      }
    },
    xAxis: {
      type: "category",
      boundarGap: false,
      data: props.optionData.configList.map(item => item.displayName) || [],
      axisTick: {
        alignWithLabel: true
      }
    },
    yAxis: yAxis,
    series: series
  };
  return option;
});

const clickHandler = () => emits("btn-click");

const updateEchartsKey = () => {
  echartsKey.value = Math.random().toString(36).slice(2, 10);
};

type itemStyleType = {
  [key: string]: string;
};

interface dataType {
  name: string;
  value: number;
  itemStyle?: itemStyleType;
}

const getEcharts = async () => {
  await nextTick();
  return echartsComRef.value.getEcharts();
};

onMounted(async () => {
  updateEchartsKey();
});

defineExpose({
  getEcharts
});
</script>

<template>
  <div class="echarts-chart" :class="{ boxShadow, borderRidus }">
    <barTitle v-if="title" :title="title">
      <div class="model-title-slot">
        <px-button v-if="button" type="primary" size="small" plain @click="clickHandler">{{ button }}</px-button>
      </div>
    </barTitle>
    <div class="echarts-chart-com">
      <echartsCom ref="echartsComRef" :key="echartsKey" :labelLayoutFn="labelLayoutFn" :options="options" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.echarts-chart {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  width: 100%;
  min-width: v-bind(min_width);
  height: 100%;
  min-height: v-bind(min_height);
  // padding: 16px;
  background-color: #fff;

  .model-title-slot {
    display: flex;
    gap: 16px;
    align-items: center;

    .right-text {
      margin-right: 16px;
      font-weight: 600;
      line-height: 1;
      color: var(--px-color-primary);
    }
  }

  .echarts-chart-com {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: calc(100% - v-bind(title_height));
    border: 1px solid #dcdcdc;
  }
}

.boxShadow {
  box-shadow: 0 0 10px rgb(0 37 86 / 12%);
}

.borderRidus {
  border-radius: 4px;
}
</style>
